home *** CD-ROM | disk | FTP | other *** search
/ IRIX 6.5 Complementary Applications 2004 February / SGI IRIX 6.5 Complementary Applications 2004 February.iso / dist / cde.idb / usr / dt / share / examples / dtscreen / screensaver.c.z / screensaver.c
Encoding:
C/C++ Source or Header  |  2003-11-18  |  6.5 KB  |  192 lines

  1. /*
  2.  * screensaver.c
  3.  *
  4.  * Copyright 2000, Silicon Graphics, Inc.
  5.  * ALL RIGHTS RESERVED
  6.  * 
  7.  * UNPUBLISHED -- Rights reserved under the copyright laws of the United
  8.  * States.   Use of a copyright notice is precautionary only and does not
  9.  * imply publication or disclosure.
  10.  *
  11.  * U.S. GOVERNMENT RESTRICTED RIGHTS LEGEND:
  12.  * Use, duplication or disclosure by the Government is subject to restrictions
  13.  * as set forth in FAR 52.227.19(c)(2) or subparagraph (c)(1)(ii) of the Rights
  14.  * in Technical Data and Computer Software clause at DFARS 252.227-7013 and/or
  15.  * in similar or successor clauses in the FAR, or the DOD or NASA FAR
  16.  * Supplement.  Contractor/manufacturer is Silicon Graphics, Inc.,
  17.  * 2011 N. Shoreline Blvd. Mountain View, CA 94039-7311.
  18.  *
  19.  * THE CONTENT OF THIS WORK CONTAINS CONFIDENTIAL AND PROPRIETARY
  20.  * INFORMATION OF SILICON GRAPHICS, INC. ANY DUPLICATION, MODIFICATION,
  21.  * DISTRIBUTION, OR DISCLOSURE IN ANY FORM, IN WHOLE, OR IN PART, IS STRICTLY
  22.  * PROHIBITED WITHOUT THE PRIOR EXPRESS WRITTEN PERMISSION OF SILICON
  23.  * GRAPHICS, INC.
  24.  */
  25. /* $XConsortium: screensaver.c /main/cde1_maint/1 1995/07/17 16:44:35 drk $ */
  26. /************************************/
  27. /** Sample CDE Screen Saver Client **/
  28. /************************************/
  29.  
  30. #include <locale.h>
  31. #include <stdio.h>
  32. #include <sys/time.h>
  33. #include <math.h>
  34. #include <X11/Xlib.h>
  35. #include <X11/Intrinsic.h>
  36. #include <Dt/Saver.h>
  37.  
  38. #define SS_MAX_COLORS 6
  39.  
  40. typedef struct {
  41.     Window    id;
  42.     int        width;
  43.     int        height;
  44.     unsigned long ssPixels[SS_MAX_COLORS];
  45.     Colormap    cmap;
  46.     GC        gc;
  47. } WinDataStruct;
  48.  
  49. int
  50. usleep(usec)
  51.     unsigned long usec;
  52. {
  53.     poll((struct poll *) 0, (size_t) 0, usec / 1000);   /* milliseconds */
  54.     return 0;
  55. }
  56.  
  57. int main(int argc, char *argv[])
  58. {
  59. Display *dpy;            /* Display connection      */
  60. Window *winprop = NULL;        /* dtsession cover windows */
  61. int nWindows;            /* number of windows       */
  62. WinDataStruct *perWindow;    /* per-window information  */
  63. int i, j;            /* index variable       */
  64. XColor actual, exact;        /* color structs       */
  65. int colorRotate = 0;        /* color rotation counter  */
  66.  
  67.     setlocale(LC_ALL, "");
  68.  
  69.     /*******************************/
  70.     /** open a display connection **/
  71.     /*******************************/
  72.     if (!(dpy = XOpenDisplay(NULL)) ) {
  73.     fprintf(stderr, "Unable to open the Display.\n");
  74.     exit(1);
  75.     }
  76.  
  77.     /***********************************************************/
  78.     /** Get the list of screen saver windows from the desktop **/
  79.     /***********************************************************/
  80.     if (!DtSaverGetWindows(dpy, &winprop, &nWindows)) {
  81.     fprintf(stderr, "Unable to get screen saver info.\n");
  82.     XCloseDisplay(dpy);
  83.     exit(1);
  84.     }
  85.  
  86.     /******************************************************/
  87.     /** allocate an array to hold per window information **/
  88.     /******************************************************/
  89.     if ( (perWindow = (WinDataStruct *)malloc(nWindows * sizeof(WinDataStruct)))
  90.     == NULL) {
  91.     fprintf(stderr, "Out of memory.\n");
  92.     XCloseDisplay(dpy);
  93.     exit(1);
  94.     }
  95.  
  96.     /*****************************************************/
  97.     /** get things set up for each window we were given **/
  98.     /*****************************************************/
  99.     for (i = 0; i < nWindows; i++) {
  100.     XWindowAttributes attr;
  101.  
  102.     perWindow[i].id = winprop[i];
  103.     /*************************************/
  104.     /** get information for each window **/
  105.     /*************************************/
  106.     if (! XGetWindowAttributes(dpy, perWindow[i].id, &attr)) {
  107.         fprintf(stderr, "Unable to get window %d attributes.\n",
  108.                     perWindow[i].id);
  109.             free((void *)perWindow);
  110.             XCloseDisplay(dpy);
  111.             exit(1);
  112.     }
  113.  
  114.     /***************************************/
  115.     /** save the window info we will need **/
  116.     /***************************************/
  117.     perWindow[i].width = attr.width;
  118.     perWindow[i].height = attr.height;
  119.     perWindow[i].cmap = DefaultColormapOfScreen(attr.screen);
  120.     perWindow[i].gc = DefaultGCOfScreen(attr.screen);
  121.  
  122.     /***********************************************/
  123.     /** Allocate the colors we will use, fallback **/
  124.     /** to black and white if necessary           **/
  125.     /***********************************************/
  126.     if (XAllocNamedColor(dpy,perWindow[i].cmap,"red",&actual,&exact)){
  127.         perWindow[i].ssPixels[0] = actual.pixel;
  128.     } else {
  129.         perWindow[i].ssPixels[0] = BlackPixelOfScreen(attr.screen);
  130.     }
  131.         if (XAllocNamedColor(dpy,perWindow[i].cmap,"orange",&actual,&exact)){
  132.             perWindow[i].ssPixels[1] = actual.pixel;
  133.         } else {
  134.             perWindow[i].ssPixels[1] = WhitePixelOfScreen(attr.screen);
  135.         }
  136.         if (XAllocNamedColor(dpy,perWindow[i].cmap,"yellow",&actual,&exact)){
  137.             perWindow[i].ssPixels[2] = actual.pixel;
  138.         } else {
  139.             perWindow[i].ssPixels[2] = BlackPixelOfScreen(attr.screen);
  140.         }
  141.     if (XAllocNamedColor(dpy,perWindow[i].cmap,"green",&actual,&exact)){
  142.                 perWindow[i].ssPixels[3] = actual.pixel;
  143.         } else {
  144.                 perWindow[i].ssPixels[3] = WhitePixelOfScreen(attr.screen);
  145.         }
  146.     if (XAllocNamedColor(dpy,perWindow[i].cmap,"blue",&actual,&exact)){
  147.                 perWindow[i].ssPixels[4] = actual.pixel;
  148.         } else {
  149.                 perWindow[i].ssPixels[4] = BlackPixelOfScreen(attr.screen);;
  150.         }
  151.         if (XAllocNamedColor(dpy,perWindow[i].cmap,"purple",&actual,&exact)){
  152.             perWindow[i].ssPixels[5] = actual.pixel;
  153.         } else {
  154.             perWindow[i].ssPixels[5] = WhitePixelOfScreen(attr.screen);
  155.         }
  156.     }
  157.  
  158.     /************************************/
  159.     /** OK, now enter our drawing loop **/
  160.     /************************************/
  161.     while (1) {
  162.     /************************/
  163.     /** update each window **/
  164.     /************************/
  165.     for (i = 0; i < nWindows; i++) {
  166.         /***************************/
  167.         /** for each color we use **/
  168.         /***************************/
  169.         for (j = 0; j < SS_MAX_COLORS; j++) {
  170.         int x, y, width, height;
  171.         unsigned long curColor;
  172.  
  173.         curColor = perWindow[i].ssPixels[
  174.                 (int)fmod((j + colorRotate),SS_MAX_COLORS)];
  175.         XSetBackground(dpy, perWindow[i].gc, curColor);
  176.         XSetForeground(dpy, perWindow[i].gc, curColor);
  177.         x = j * ((perWindow[i].width/2) / (SS_MAX_COLORS));
  178.         y = j * ((perWindow[i].height/2) / (SS_MAX_COLORS));
  179.         width = perWindow[i].width - (2 * x);
  180.         height = perWindow[i].height - (2 * y);
  181.         XFillRectangle(dpy, perWindow[i].id, perWindow[i].gc,
  182.                    x, y, width, height);
  183.         }
  184.     }
  185.     /**XFlush(dpy); **/
  186.     XSync(dpy, False);
  187.     colorRotate = (int) fmod ((colorRotate + 1), SS_MAX_COLORS);
  188.  
  189.     usleep(200000);
  190.     }
  191. }
  192.